<?php /* PROJECTS $Id: reports.php,v 1.12.2.3 2007/01/31 09:36:52 ajdonnison Exp $ */
define("MANAGER", 5);
//error_reporting( E_ALL );

if (!defined('DP_BASE_DIR')) {
	die('You should not access this file directly');
}

$project_id = intval( dPgetParam( $_REQUEST, "project_id", 0 ) );
$report_type = dPgetParam( $_REQUEST, "report_type", '' );

// check permissions for this record
$perms =& $AppUI->acl();

$obj = new CProject();
$obj->load($project_id);

$canRead = $perms->checkModuleItem( $m, 'view', $project_id ) &&
	($AppUI->user_id == $obj->project_owner || $AppUI->user_type <= MANAGER);

if (!$canRead) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

$display_project_name = $obj->project_name;

if (! $suppressHeaders) {
?>
<script language="javascript">

function changeIt() {
        var f=document.changeMe;
        f.submit();
}
</script>

<?php
}
// get the prefered date format
$df = $AppUI->getPref('SHDATEFORMAT');

$reports = $AppUI->readFiles( dPgetConfig( 'root_dir' )."/modules/projects/reports", "\.php$" );

// setup the title block
if (! $suppressHeaders) {
	$titleBlock = new CTitleBlock( 'Project Reports' );
	$titleBlock->addButton( "projects list", 'index.php?m=projects' );
	$titleBlock->show();
}

$report_type_var = dPgetParam($_GET, 'report_type', '');

if (!empty($report_type_var)) {
	$report_type_var = '&report_type=' . $report_type;
}

$title_ext = '';
if ($report_type != '') {
	$rfx = DP_BASE_DIR."/modules/projects/reports/".$report_type.'.'.$AppUI->user_locale.'.txt';
	if (file_exists($rfx)) {
		$ds = file( $rfx);
		$title_ext = '  -  '.$ds[0];
	}
}

if (! $suppressHeaders) {
if (!isset($display_project_name))
	$display_project_name = $AppUI->_('All');
?>
<table cellspacing="0" cellpadding="4" border="0" width="100%">
<tr>
	<td class="tabon">
		<?
			echo $display_project_name.$title_ext;
		?>
	</td>
	<td width="100%">&nbsp;</td>
</tr>
</table>
<table class="infopanel" cellspacing="0" cellpadding="4" border="0" width="100%">
<tr>
	<td nowrap="nowrap">
	<?
		showButton("view this project", "?m=projects&a=view&project_id=$project_id");
		echo '&nbsp;';
		if ($report_type) {
			showButton( "reports index", "index.php?m=projects&a=reports&project_id=$project_id" );
		}
	?>
	</td>
	<td width="100%">&nbsp;</td>
</tr>
</table>
<?php
}
if ($report_type) {
	$report_type = $AppUI->checkFileName( $report_type );
	$report_type = str_replace( ' ', '_', $report_type );
	require DP_BASE_DIR."/modules/projects/reports/$report_type.php";
} else {
	echo '<table id="idReportList" class="list" cellspacing="0" cellpadding="4" border="0" width="100%">';
	echo '<tr><th>&nbsp;</th><th>' . $AppUI->_( 'Report' ) . '</th><th class="last">'. $AppUI->_( 'Description' ) .'</th></tr>';
	$nr = 1;
	foreach ($reports as $v) {
		$type = str_replace( ".php", "", $v );
		$desc_file = str_replace( ".php", ".{$AppUI->user_locale}.txt", $v );
		$desc = @file( DP_BASE_DIR."/modules/projects/reports/$desc_file");

		$rd = (@$desc[0] ? $desc[0] : $v);
		$href = "index.php?m=projects&a=reports&project_id=$project_id&report_type=$type";
		if (isset($desc[2])) $href .= '&' . trim($desc[2]);

		echo '<tr class="'.($nr % 2 == 1 ? 'first' : 'second').'" onclick="location.href=\''.$href.'\'">'.
			 '<td align="center">'.($nr++).'</td>';
		echo '<td>' . $rd . '</td>';
		echo '<td>' . (@$desc[1] ? $desc[1] : '') . '</td>';
		echo '</tr>';
	}
	echo '</table>';
}
?>
<script>
function viewReport(r, d) {
	location.href = 'index.php?m=projects&a=reports&project_id=<?=$project_id?>&report_type='+r;
}

Table('idReportList').setNormalHover(1);
</script>
